#_*_coding:utf-8_*_
from daterange_filter.filter import DateRangeFilter
from models import Conf_record,Cabinet, Ip_details, Board_card, Cpu, Conf, Server, HostGroup, Record, Blogs, Ticket, Disk, Disk_rec, Nic, Nic_rec, Memery, Memery_rec, Contract, Menber, Ivn_server, Supplier
from django.contrib import admin
from django.contrib import admin
##########
from django.contrib.admin import SimpleListFilter

def make_false(modelAdmin, request, queryset): #queryset：选中的集合；modelAdmin代表BookAdmin类,相当于self
    make_false.short_description = "设置为未完成"
#    print("----->", modelAdmin,request,queryset)
    queryset.update(ticket_state=0) #更改选中的为完成

def make_ture(modelAdmin, request, queryset):
    make_ture.short_description = "设置为已完成"
    queryset.update(ticket_state=1) #更改选中的为未完成

def make_on(modelAdmin, request, queryset):
    make_ture.short_description = "设置为监控"
    queryset.update(check=1)

def make_off(modelAdmin, request, queryset):
    make_ture.short_description = "设置为不监控"
    queryset.update(check=0)

#class RecordInline(admin.StackedInline): //占屏模式


#################TabularInline##########################
#class ConfInline(admin.TabularInline):
#    model = Conf
#    extra = 1
class Conf_recordInline(admin.TabularInline):
    model = Conf_record
    extra = 1
class RecordInline(admin.TabularInline):
    model = Record
    extra = 1
class Disk_recInline(admin.TabularInline):
    model = Disk_rec
    extra = 1
class Memery_recInline(admin.TabularInline):
    model = Memery_rec
    extra = 1
class Nic_recInline(admin.TabularInline):
    model = Nic_rec
    extra = 1

class CpuInline(admin.TabularInline):
    model = Cpu
    extra = 1
class DiskInline(admin.TabularInline):
    model = Disk
    extra = 1
class MemeryInline(admin.TabularInline):
    model = Memery
    extra = 1
class NicInline(admin.TabularInline):
    model = Nic
    extra = 1
class Board_cardInline(admin.TabularInline):
    model = Board_card
    extra = 1


#######################ModelAdmin#############################
#class UploadAdmin(admin.ModelAdmin):
#    list_display = [
#    'title','file'
#    ]
class Ip_detailsAdmin(admin.ModelAdmin):
    list_display = [
    'manager_ip','manager_ip_netmask','manager_ip_gateway','ip','ip_netmask','ip_gateway'
    ]

class CabinetAdmin(admin.ModelAdmin):
    list_display = [
    'cabinet_list',
    'cabinet_number',
    'cabinet_begin',
    ]

class ConfAdmin(admin.ModelAdmin):
    list_display = [
    'name','description', 'price','supplier'
    ]
#    inlines = [Spare_detailsInline]
    inlines = [DiskInline,MemeryInline,NicInline,CpuInline,Board_cardInline]
class RecordAdmin(admin.ModelAdmin):
    list_display = [
    'fault_type', 'state', 'discover_time', 'report_time', 'report_person', 'end_time', 'repair_person', 'res', 'memo'
    ]

class MenberAdmin(admin.ModelAdmin):
    list_display = [
    'name', 'phone', 'email'
    ]

class TicketAdmin(admin.ModelAdmin):
    actions = [make_ture, make_false]
    list_display = [
    'ticket_content','ticket_time','ticket_type', 'executor', 'get_menber','server_number','ticket_state','end_time'
    ]
    search_fields = ['remark','ticket_content', 'ticket_type', 'executor', 'menber__name', 'ticket_state']
    list_filter = [('ticket_time', DateRangeFilter),'ticket_type','executor','menber__name']


class SupplierAdmin(admin.ModelAdmin):
    list_display = [
    'name','bank','bank_id','contact','phone','address','address2'
    ]
#class DiskAdmin(admin.ModelAdmin):
#    list_display = [
#    'disk_type','disk_rev','disk_size','supplier','disk_num'
#    ]
#    inlines = [Disk_recInline]

#class MemeryAdmin(admin.ModelAdmin):
#    list_display = [
#    'mem_type','mem_size','supplier','mem_num'
#    ]
#    inlines = [Memery_recInline]

#class NicAdmin(admin.ModelAdmin):
#    list_display = [
#    'nic_speed','nic_type','nic_pci','supplier','nic_num'
#    ]
#    inlines = [Nic_recInline]
class ContractAdmin(admin.ModelAdmin):
    list_display = [
    'csn','jiafang','yifang','cname','producer','price','detail','contract_begin_date','contract_end_date','begin_date','end_date'
    ]
#    inlines = [ConfInline]
#    inlines = [Inline]
class Ivn_serverAdmin(admin.ModelAdmin):
    list_display = [
    'sn',
    'brand',
    'contract',
    'change_times',
    'change_stat',
    'rec_person',
    'change_num',
    'ivn_num',
    'remarks'
    ]
    fieldsets = [
        ('带外信息', { 'fields': ['sn', 'contract', 'brand','change_times','change_stat','rec_person','change_num','ivn_num', 'remarks' ]}),
    ]
    search_fields = ['sn']
class ServerAdmin(admin.ModelAdmin):
    list_display = [
    'sn',
#    'manager_ip__manager_ip',
    'ip_details',
    'check',
    'idc_mod',
    'cabinet',
    'owner',
    'status',
    'department',
    'business',
#    'remarks'
    ]
    actions = [make_on, make_off]
    fieldsets = [
        ('带外信息', { 'fields': ['sn', 'ip_details', 'idc_mod', 'cabinet', 'contract', 'status', 'remarks' ]}),
        ('详细信息', {'classes' : ('collapse',),'fields': ['check', 'conf', 'hostname', 'password', 'os', 'release', 'owner', 'create_date', 'machine_type', 'department', 'business']}),
    ]
    search_fields = ['sn', 'ip_details__manager_ip', 'business', 'remarks']
    list_filter = ['idc_mod', 'department', 'owner', 'cabinet', 'cabinet__cabinet_begin', 'cabinet__cabinet_number', 'business']
    inlines = [RecordInline,Conf_recordInline]
#    date_hierarchy = 'create_date'
#    ordering = ('-create_date',)

class HostGroupAdmin(admin.ModelAdmin):
    list_display = ['name']

class BlogPostAdmin(admin.ModelAdmin):
    list_display = ['title','ctime','get_author']

#admin.site.register(Upload, UploadAdmin)
admin.site.register(Cabinet, CabinetAdmin)
#admin.site.register(Spare_details, Spare_detailsAdmin)
admin.site.register(Ip_details, Ip_detailsAdmin)
admin.site.register(Conf, ConfAdmin)
admin.site.register(Supplier, SupplierAdmin)
admin.site.register(Server, ServerAdmin)
admin.site.register(Record, RecordAdmin)
admin.site.register(Contract, ContractAdmin)
admin.site.register(Ticket, TicketAdmin)
#admin.site.register(Disk, DiskAdmin)
#admin.site.register(Nic, NicAdmin)
#admin.site.register(Memery, MemeryAdmin)
admin.site.register(Blogs ,BlogPostAdmin)
admin.site.register(Menber ,MenberAdmin)
admin.site.register(Ivn_server, Ivn_serverAdmin)
